package org.gesplan.controller;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.gesplan.exceptions.NonexistentEntityException;
import org.gesplan.entity.Building;
import org.gesplan.entity.MaterialBuilding;
import org.gesplan.util.Utils;


/**
 *
 * @author David
 */
public class MaterialBuildingJpaController {

    private EntityManagerFactory emf = null;
    
    
    public MaterialBuildingJpaController() {
        this.emf = Utils.getEntityManagerFactory();
    }


    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }


    public void create(MaterialBuilding materialBuilding) {
        
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(materialBuilding);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        
    }


    public void edit(MaterialBuilding materialBuilding) throws NonexistentEntityException, Exception {
        
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            MaterialBuilding persistentMaterialBuilding = em.find(MaterialBuilding.class, materialBuilding.getId());
            Building buildingIdOld = persistentMaterialBuilding.getBuildingId();
            Building buildingIdNew = materialBuilding.getBuildingId();
            if (buildingIdNew != null) {
                buildingIdNew = em.getReference(buildingIdNew.getClass(), buildingIdNew.getId());
                materialBuilding.setBuildingId(buildingIdNew);
            }
            materialBuilding = em.merge(materialBuilding);
            if (buildingIdOld != null && !buildingIdOld.equals(buildingIdNew)) {
                buildingIdOld.getMaterialBuildingCollection().remove(materialBuilding);
                buildingIdOld = em.merge(buildingIdOld);
            }
            if (buildingIdNew != null && !buildingIdNew.equals(buildingIdOld)) {
                buildingIdNew.getMaterialBuildingCollection().add(materialBuilding);
                em.merge(buildingIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = materialBuilding.getId();
                if (findMaterialBuilding(id) == null) {
                    throw new NonexistentEntityException("The materialBuilding with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
        
    }


    public void destroy(Integer id) throws NonexistentEntityException {
        
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            MaterialBuilding materialBuilding;
            try {
                materialBuilding = em.getReference(MaterialBuilding.class, id);
                materialBuilding.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The materialBuilding with id " + id + " no longer exists.", enfe);
            }
            Building buildingId = materialBuilding.getBuildingId();
            if (buildingId != null) {
                buildingId.getMaterialBuildingCollection().remove(materialBuilding);
                em.merge(buildingId);
            }
            em.remove(materialBuilding);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        
    }


    public List<MaterialBuilding> findMaterialBuildingEntities() {
        return findMaterialBuildingEntities(true, -1, -1);
    }


    public List<MaterialBuilding> findMaterialBuildingEntities(int maxResults, int firstResult) {
        return findMaterialBuildingEntities(false, maxResults, firstResult);
    }

    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private List<MaterialBuilding> findMaterialBuildingEntities(boolean all, int maxResults, int firstResult) {
        
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(MaterialBuilding.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
        
    }


    public MaterialBuilding findMaterialBuilding(Integer id) {
        
        EntityManager em = getEntityManager();
        try {
            return em.find(MaterialBuilding.class, id);
        } finally {
            em.close();
        }
        
    }

    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public int getMaterialBuildingCount() {
        
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<MaterialBuilding> rt = cq.from(MaterialBuilding.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
        
    }
   
    
}